<template>
  <div>
    <a-card>
      <a-button icon="plus" type="primary" style="margin-bottom:24px" @click="handleAdd">新增</a-button>
      <s-table ref="table" size="default" :rowKey="record => record.docno" :columns="columns" :data="loadData" :pagination="paginationProps" style="width:1000px;margin:0 auto;">
        <span slot="action" slot-scope="text, record">
          <a-popconfirm title="确定删除吗?" ok-text="Yes" cancel-text="No" @confirm="handleDel(record)">
            <a href="#">删除</a>
          </a-popconfirm>
        </span>
      </s-table>
    </a-card>
    <a-modal v-model="visible" title="新增" @ok="handleOk" @cancel="handleCancel" destroyOnClose>
      <a-form-model-item label="策略编号">
        <a-input v-model="value" />
      </a-form-model-item>
    </a-modal>
  </div>
</template>
<script>
import { listMixin } from '../util/listMixin'
import { shieldStrategyList, shieldStrategyAdd, shieldStrategyDel } from '@/api/box/setting'
const columns = [
  { title: '策略编号', align: 'center', dataIndex: 'docno' },
  { title: '操作', align: 'center', scopedSlots: { customRender: 'action' } }
]
export default {
  mixins: [listMixin],
  data() {
    return {
      columns,
      value: '',
      visible: false
    }
  },
  computed: {
    loadData() {
      return parameter => this.fetchData(parameter)
    }
  },
  methods: {
    fetchData(parameter) {
      let params = {
        page: parameter
      }
      return this.fetchList(params, shieldStrategyList).then(res => {
        if (res.code == 0) {
          return res
        } else {
          this.$message.warning(res.msg)
          return {
            data: [],
            page: {
              pageNo: 1,
              pageSize: 10,
              count: 10
            }
          }
        }
      })
    },
    handleDel(record) {
      shieldStrategyDel(record.docno).then(res => {
        if (res.code == 0) {
          this.$message.success('删除成功')
          this.$refs.table.refresh()
        } else {
          this.$message.warning(res.msg)
        }
      })
    },
    handleAdd() {
      this.visible = true
    },
    handleOk() {
      if (!this.value) {
        this.$message.warning('请输入')
        return
      }
      shieldStrategyAdd(this.value).then(res => {
        if (res.code == 0) {
          this.$message.success('新增成功')
          this.$refs.table.refresh(true)
          this.handleCancel()
        } else {
          this.$message.warning(res.msg)
        }
      })
    },
    handleCancel() {
      this.visible = false
      this.value = ''
    }
  }
}
</script>

